Since you put "use strict" inside the BEGIN block, it didn't tell you that
%t is not defined in the first oneliner outside that block. If you did perl
-Mstrict -MData::Dumper ...., you'd  get a better error message.

How come the second example is any different? Well, it shouldn't be, but
"$a" happens to be a magical name. Don't believe me? Try the same code with
$baz; it'll fail.

$a and $b are special package vars that Perl allows you to use without
declaration even when use strict is in place. The reason they exist is that
you can write comparators succinctly (e.g., sort { $b <=> $a } @list).

On Wed, Jul 27, 2011 at 3:59 PM, Avishalom Shalit <[email protected]>wrote:

> head file | perl -MData::Dumper  -wple 'BEGIN{use strict;my %t=(); my
> @months=qw(jan feb mar apr may jun jul aug sep oct nov dec);@t{map
> uc,@months}=1..12;$a=\%t;} ;  s/(?<=\d\d-)(...)(?=-\d\d)/$t{"$1"}/;'
>
> fails (specifically JUN is replaced with an empty string)
>
> $ head -2 U__E__103.tsv | perl -MData::Dumper  -wple 'BEGIN{use
> strict;my %t=(); my @months=qw(jan feb mar apr may jun jul aug sep oct
> nov dec);@t{map uc,@months}=1..12;$a=\%t;} ;
> s/(?<=\d\d-)(...)(?=-\d\d)/$a->{"$1"}/;'
>
> works
>
>
> -- vish
>
>
>
>
>
> On 27 July 2011 12:55, Gaal Yahas <[email protected]> wrote:
> > Please paste the command you're trying. Hash subscript works for me.
> >   $ perl -le '%t = (a => "b", c => "d"); print $t{a}'
> >   b
> > On Wed, Jul 27, 2011 at 2:05 PM, Avishalom Shalit <[email protected]>
> > wrote:
> >>
> >> to reiterate the problem
> >>
> >> $t{something}
> >> is interpreted as ($t) ({something})
> >>
> >> in a one liner
> >>
> >> (not in a script)
> >> -- vish
> >>
> >
> > --
> > Gaal Yahas <[email protected]>
> > http://gaal.livejournal.com/
> >
> > _______________________________________________
> > Perl mailing list
> > [email protected]
> > http://mail.perl.org.il/mailman/listinfo/perl
> >
> _______________________________________________
> Perl mailing list
> [email protected]
> http://mail.perl.org.il/mailman/listinfo/perl
>



-- 
Gaal Yahas <[email protected]>
http://gaal.livejournal.com/
_______________________________________________
Perl mailing list
[email protected]
http://mail.perl.org.il/mailman/listinfo/perl

Reply via email to