perl

What is happening when my() is conditional?

a 夏天 提交于 2021-02-07 11:22:05
问题 Compare using perl -w -Mstrict : # case Alpha print $c; ... # case Bravo if (0) { my $c = 1; } print $c; ... # case Charlie my $c = 1 if 0; print $c; Alpha and Bravo both complain about the global symbol not having an explicit package name, which is to be expected. But Charlie does not give the same warning, only that the value is uninitialized, which smells a lot like: # case Delta my $c; print $c; What exactly is going on under the hood? (Even though something like this should never be

Print all lines between two patterns, exclusive, first instance only (in sed, AWK or Perl) [duplicate]

白昼怎懂夜的黑 提交于 2021-02-07 09:33:34
问题 This question already has answers here : How to print lines between two patterns, inclusive or exclusive (in sed, AWK or Perl)? (9 answers) Closed 1 year ago . Using sed, AWK (or Perl), how do you print all lines between (the first instance of) two patterns, exclusive of the patterns? 1 That is, given as input: aaa PATTERN1 bbb ccc ddd PATTERN2 eee Or possibly even: aaa PATTERN1 bbb ccc ddd PATTERN2 eee fff PATTERN1 ggg hhh iii PATTERN2 jjj I would expect, in both cases: bbb ccc ddd 1 A

How can log4perl write to STDERR and a file at the same time?

房东的猫 提交于 2021-02-07 09:01:14
问题 I tried to set up two appenders, but it seems to only write to STDERR: my $header = "######$scriptname $version"; use Log::Log4perl qw(:easy); Log::Log4perl->easy_init($DEBUG); my $logger = get_logger(); my $layout = Log::Log4perl::Layout::PatternLayout->new( "%d %p> %F{1}:%L %M - %m%n"); my $appender = Log::Log4perl::Appender->new( "Log::Dispatch::File", filename=>$scriptname.".log", mode => "append" ); $appender->layout($layout); my $stderr = Log::Log4perl::Appender::Screen->new( stderr =>0

How can log4perl write to STDERR and a file at the same time?

拟墨画扇 提交于 2021-02-07 09:00:33
问题 I tried to set up two appenders, but it seems to only write to STDERR: my $header = "######$scriptname $version"; use Log::Log4perl qw(:easy); Log::Log4perl->easy_init($DEBUG); my $logger = get_logger(); my $layout = Log::Log4perl::Layout::PatternLayout->new( "%d %p> %F{1}:%L %M - %m%n"); my $appender = Log::Log4perl::Appender->new( "Log::Dispatch::File", filename=>$scriptname.".log", mode => "append" ); $appender->layout($layout); my $stderr = Log::Log4perl::Appender::Screen->new( stderr =>0

How can log4perl write to STDERR and a file at the same time?

假如想象 提交于 2021-02-07 08:59:25
问题 I tried to set up two appenders, but it seems to only write to STDERR: my $header = "######$scriptname $version"; use Log::Log4perl qw(:easy); Log::Log4perl->easy_init($DEBUG); my $logger = get_logger(); my $layout = Log::Log4perl::Layout::PatternLayout->new( "%d %p> %F{1}:%L %M - %m%n"); my $appender = Log::Log4perl::Appender->new( "Log::Dispatch::File", filename=>$scriptname.".log", mode => "append" ); $appender->layout($layout); my $stderr = Log::Log4perl::Appender::Screen->new( stderr =>0

Convert array of hashes to json

℡╲_俬逩灬. 提交于 2021-02-07 08:32:26
问题 I want to convert an array of hashes that I create like this: while(...) { ... push(@ranks, {id => $id, time => $time}); } To JSON: use JSON; $j = new JSON; print $j->encode_json({ranks => @ranks}); But it is outputting this: {"ranks":{"time":"3","id":"tiago"}, "HASH(0x905bf70)":{"time":"10","id":"bla"}} As you can see it isnt able to write on of the hashes and there's no array... I would like to output a JSON string that looked like this: {"ranks":[{"time":"3","id":"tiago"}, {"time":"40","id

Convert array of hashes to json

喜欢而已 提交于 2021-02-07 08:31:11
问题 I want to convert an array of hashes that I create like this: while(...) { ... push(@ranks, {id => $id, time => $time}); } To JSON: use JSON; $j = new JSON; print $j->encode_json({ranks => @ranks}); But it is outputting this: {"ranks":{"time":"3","id":"tiago"}, "HASH(0x905bf70)":{"time":"10","id":"bla"}} As you can see it isnt able to write on of the hashes and there's no array... I would like to output a JSON string that looked like this: {"ranks":[{"time":"3","id":"tiago"}, {"time":"40","id

Perl phone-number regex

江枫思渺然 提交于 2021-02-07 08:18:49
问题 Sorry for asking such a simple question, I'm still an inexperienced programmer. I stumbled across a phone-number-matching regex in some old perl code at work, I'd love it if somebody could explain exactly what it means (my regex skills are severely lacking). if ($value !~ /^\+[[:space:]]*[0-9][0-9.[:space:]-]*(\([0-9.[:space:]-]*[0-9][0-9.[:space:]-]*\))?([0-9.[:space:]-]*[0-9][0-9.[:space:]-]*)?([[:space:]]+ext.[0-9.[:space:]-]*[0-9][0-9.[:space:]-]*)?$/i) { ... } Thank you in advance :) 回答1

Perl phone-number regex

a 夏天 提交于 2021-02-07 08:18:30
问题 Sorry for asking such a simple question, I'm still an inexperienced programmer. I stumbled across a phone-number-matching regex in some old perl code at work, I'd love it if somebody could explain exactly what it means (my regex skills are severely lacking). if ($value !~ /^\+[[:space:]]*[0-9][0-9.[:space:]-]*(\([0-9.[:space:]-]*[0-9][0-9.[:space:]-]*\))?([0-9.[:space:]-]*[0-9][0-9.[:space:]-]*)?([[:space:]]+ext.[0-9.[:space:]-]*[0-9][0-9.[:space:]-]*)?$/i) { ... } Thank you in advance :) 回答1

How can I read the files in a directory in sorted order?

我的梦境 提交于 2021-02-07 06:15:14
问题 When I read a directory in Perl with opendir , readdir , and closedir , the readdir function doesn't seem to read the files in any specific order (that I can tell). I am reading a directory that has subdirectories named by epoch timestamp: 1224161460 1228324260 1229698140 I want to read in these directories in numerical order, which would put the oldest directories first. When I use readdir , the first one it reads is 1228324260, which is the middle one. I know I could put the directory