perl514 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, First off, my apologies if my question is phrased wrong... I am trying to work on the following script:
#!/usr/bin/perl use warnings; use strict; #My First Reference called ref1.pl!! my $array = [`DIR`]; print "@{$array}\n";
Thing is, the moment I hit enter after typing  perl ref1.pl, it takes about 3 to 4 seconds for the output to appear. So I tried using the debugger and found out that the seconds are consumed at the line where  my $array = [`DIR`]; appears....
C:\perl\practice>perl -d ref1.pl Loading DB routines from perl5db.pl version 1.33 Editor support available. Enter h or `h h' for help, or `perldoc perldebug' for more help. main::(ref1.pl:5): my $array = [`DIR`]; DB<1> l 5==> my $array = [`DIR`]; 6: print "@{$array}\n"; DB<1> s
This is where it takes about 3 seconds and then the following line appears. After that, everything is put out on the screen pretty fast.
main::(ref1.pl:6): print "@{$array}\n"; DB<1> s main::(ref1.pl:6): print "@{$array}\n"; DB<1> s Volume in drive C is PC COE Volume Serial Number is 2EB6-FC84 Directory of C:\perl\practice 12/11/2011 12:39 AM <DIR> . 12/11/2011 12:39 AM <DIR> .. 12/09/2011 12:27 AM 303 each.pl 12/09/2011 12:16 AM 271 hashcolor.pl 12/10/2011 01:31 PM 468 Perl-1.pl 12/10/2011 02:13 PM 534 Perl-2.pl 12/10/2011 05:28 PM 223 Perl-4.pl 12/09/2011 12:34 AM 301 Perl-5.pl 12/11/2011 12:39 AM 112 ref1.pl 7 File(s) 2,212 bytes 2 Dir(s) 445,457,670,144 bytes free Debugged program terminated. Use q to quit or R to restart, use o inhibit_exit to avoid stopping after program termination, h q, h R or h o to get additional info. DB<1> s Use `q' to quit or `R' to restart. `h q' for details. DB<1> q C:\perl\practice>
Is this expected behaviour when I use an anonymous reference? Am I doing something wrong here? I have installed 32 bit Activestate Perl on my 64 Bit Windows 7 laptop. Could that be a reason? I have the 64 bit version too, but since the servers on which I plan to run my scripts from is 32 bits, I thought its better to install a 32 bit version. Please let me know.

Replies are listed 'Best First'.
Re: why is my reference taking a while to execute?
by chromatic (Archbishop) on Dec 10, 2011 at 20:03 UTC

    You're making a system call which performs disk IO; that's likely the slow part. What happens if you build an array without making a system call or performing disk IO? If that also takes three seconds, then something is weird. Otherwise, your code behaves exactly as I expect.


    Improve your skills with Modern Perl: the free book.

      Hi Chromatic, Yessir. You are right. I tried the following and that output didnt take time. so this:
      #!/usr/bin/perl use warnings; use strict; #My First Reference called ref1.pl!! my $array = [qw(this is a test to check anonymous arrays)]; print "@{$array}\n"; print "$array->[3]\n"
      gave the following output in no time.
      C:\perl\practice>perl ref2.pl this is a test to check anonymous arrays test

      Well, if the disk i/os are causing that then that could be a problem for me..... I wanted to create a refence to an anonymous array so that it can hold command line outputs and I can play around with it.....I tried it with  ping localhost command and that took even longer...say about 6 seconds..... May be I shouldnt use reference to anonymous arrays for storing command line outputs..cause that wont be a good way to deal with it.

      Note - I wanted to make an anonymous array with words in it...I tried  qw [this is a test to check anonymous arrays]but Perl told me something like "Useless use of a constant..."...so I tried some other ways and finally tried  [qw(this is a test to check anonymous arrays)] and that seems to work.

      Is that the right way of creating a reference to an anonymous array with words in it?

        Basically, you are calling an external command, so Windows has to load up cmd.exe (dir is an inbuild command in cmd.exe as far as i know). You could very easy accomplish what you are trying to do within Perl. Doing it within perl also makes it way more portable.

        If you only want the directory listing, you probably just can use glob:

        #!/usr/bin/perl use strict; use warnings; my $files = [glob('*')]; print "@{$files}\n";

        To learn more about glob, typing perldoc -f glob should work. If not, then your ActivePerl*\bin directory is not in your PATH. From memory: Right-clicking on Computer, Properties, Extras, Advanced and then System Variables should let you edit the PATH variables. You'll have to restart open cmd.exe windows (no windows reboot required).

        Want more info about the files, like say file sizes to the file name? You could use hashes to store the data and stat to get the information. Here's a simple example:

        #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @filenames = glob('*'); my %files; foreach my $fname (@filenames) { my $size = (stat($fname))[7]; $files{$fname} = $size; } print Dumper(\%files);

        For more complicated things like recursive directory traversing you should take a look at CPAN modules like File::Util and/or File::Find.

        But in general, i endorse people new to Perl to at least once code their own recursive finder. It's a nice learning experience and nearly everyone on PerlMonks can help you out if you get stuck.

        Don't use '#ff0000':
        use Acme::AutoColor; my $redcolor = RED();
        All colors subject to change without notice.
        May be I shouldnt use reference to anonymous arrays for storing command line outputs...

        The lesson to take from this is that sometimes doing things outside of Perl can be slow. The fact of using an array isn't too relevant here.

        Is that the right way of creating a reference to an anonymous array with words in it?

        The anonymous array construction syntax of the square brackets is separate from the quoting delimiters used with qw//. What you ended up with is fine. (Print the contents of the array created from your first attempt and you'll probably see the problem.)


        Improve your skills with Modern Perl: the free book.