Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Perl Idioms Explained - ${\$obj->method} and @{[sort @list]}

by dbwiz (Curate)
on Aug 21, 2003 at 16:50 UTC ( [id://285519]=note: print w/replies, xml ) Need Help??


in reply to Perl Idioms Explained - ${\$obj->method} and @{[sort @list]}

More things that you can put inside @{[]} .

#!/usr/bin/perl -w use strict; use Data::Dumper; my $complex = { a => [ 1, 2, 3 ], b => [ { x => 1, y => 2, z => 3 }, 'alpha', [qw(even more nested ) +] ], c => { one => [qw(more value)] } }; # a function result print "@{[ eval 'Data::Dumper->Dump([$complex ],[q{complex} ])' ]}"; # a list print "@{[ ('aa'..'at') ]}\n"; # a list generated by a Regex print "@{[ 'hello Perl!' =~ /./g ]}\n"; # a list from a filehandle { local $/; print "@{[ <DATA> ]}\n"; } # (update) or simply print "@{[ <DATA> ]}\n"; # a list from globbing print "@{[ <*.pm> ]}\n"; __DATA__ This is text from DATA

This would be the resulting output (The *.pm files depend on your current dir, of course).

$complex = { 'a' => [ 1, 2, 3 ], 'b' => [ { 'x' => 1, 'y' => 2, 'z' => 3 }, 'alpha', [ 'even', 'more', 'nested' ] ], 'c' => { 'one' => [ 'more', 'value' ] } }; aa ab ac ad ae af ag ah ai aj ak al am an ao ap aq ar as at h e l l o P e r l ! This is text from DATA Basename.pm CheckTree.pm Compare.pm Copy.pm DosGlob.pm Find.pm Path.pm + Spec.pm stat.pm Temp.pm

Replies are listed 'Best First'.
Re: Re: Perl Idioms Explained - ${\$obj->method} and @{[sort @list]}
by Juerd (Abbot) on Aug 21, 2003 at 17:28 UTC

    local $/; print "@{[ <DATA> ]}\n";

    Sure, you CAN do that.

    • Creates an array
    • Fetches the file contents
    • Puts that in the new array, as one element
    • Copies the array element
    • Throws the array away
    • Adds \n
    • Passes a large string to print
    And it's a lot of work. print while <DATA>; or something similar is usually a MUCH better idea.

    Rule of thumb: do not use the idioms explained in the root of this thread, unless you really have to interpolate (like in regexes, although (??{}) may be a better idea). Especially the array creating one.

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://285519]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-03-19 11:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found