Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Broken Function

by carlriz (Beadle)
on Apr 04, 2014 at 13:08 UTC ( #1081124=perlquestion: print w/ replies, xml ) Need Help??
carlriz has asked for the wisdom of the Perl Monks concerning the following question:

I wrote this function...the concept is simple but hey I am a perl newbie, so it doesn't really work as intended. The function is supposed to be passed a path name, check to see if the file exists, and pipe the output from a cat function (of that file) to a new file (which is saved into a predetermined directory). Now, here is my broken function:

sub find_files { my $file_name = shift; my $path = qx/pwd/; print $path; my $file = basename($file_name); open(my $FILE_H, '>', "$path/$file"); #|| + + # die "Unable to open file: $!"; + + if(defined(qx/test -e $file_name; echo $?/)) { my $file_data = qx/cat $file_name/; print $FILE_H $file_data; } else { print "here"; print $FILE_H "Unable to find file name: $file_name"; } close($FILE_H); }
my @file_name_array = ( "/proc/cpuinfo", "/proc/meminfo", "/etc/fstab", "/proc/net/pf_ring/info", "/var/log/boot.log", "/etc/sysconfig/network-scripts/ifcfg-eth0", "/etc/sysconfig/network-scripts/ifcfg-eth1", "/etc/sysconfig/network-scripts/ifcfg-eth2", "/etc/sysconfig/network-scripts/ifcfg-eth3", "/etc/sysconfig/network-scripts/ifcfg-eth4", "/etc/udev/rules.d/70-persistent-net.rules" );

Comment on Broken Function
Select or Download Code
Replies are listed 'Best First'.
Re: Broken Function
by choroba (Canon) on Apr 04, 2014 at 13:21 UTC
    No need to call test to see if a file exists. We have -e in Perl, too. We even have File::Copy, so you don't need to cat a file. Path::Tiny wraps all the path related operations nicely:
    #!/usr/bin/perl # Untested! use Path::Tiny; my @files = ( ... ); sub find_files { my $filename = shift; my $path = 'Path::Tiny'->cwd; my $file = path($filename)->basename; if (-e $filename) { path($filename)->copy("$path/$file"); } else { open my $OUT, '>', "$path/$file" or die $!; print {$OUT} "Unable to find $filename\n"; } }

    Note that -e returns true for directories, too, so you might need a different test (-f maybe?). Also, you should check the return value of copy.

    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      When you Path::Tiny, to spew is lovely ,fiend :)
      else { path( "$path/$file" )->spew_utf8("Unable to find $filename\n"); }
Re: Broken Function
by Happy-the-monk (Abbot) on Apr 04, 2014 at 13:22 UTC

    Perl has a -e test builtin, no need to use the shell's test function.

    But a -r test suggests itself if you need to read the file.

    If this is not just a fiddle-with-perl exercise, what you are doing is basically copying a file. You could shortcut all of it to use File::Basename qw(basename); use File::Copy qw(copy); copy $_[0], basename $_[0];

    Cheers, Sören

    Créateur des bugs mobiles - let loose once, run everywhere.
    (hooked on the Perl Programming language)

Re: Broken Function
by BrowserUk (Pope) on Apr 04, 2014 at 13:22 UTC
    it doesn't really work as intended.

    That is a lousy error description.

    • What does it do that isn't intended?
    • And/or: What doesn't it do that is intended?

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      I get something like this: cat: /proc/cpuinfo: No such file or directory print() on closed filehandle $FILE_H at line 91. I am expecting my function to create a new file whether or not the file exists. But, my major hurdle is the second error message.

        If you had not commented out your "or die "Could not open file: $!\n" code, then the reason why the file handle did not open would have been printed.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1081124]
Approved by Happy-the-monk
Front-paged by Arunbear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (2)
As of 2016-02-07 15:26 GMT
Find Nodes?
    Voting Booth?

    How many photographs, souvenirs, artworks, trophies or other decorative objects are displayed in your home?

    Results (254 votes), past polls