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

Hi monks,

So recently I have been tasked with writing a program that will accept user input and preform the system command sha1sum on each file in the array of user input. The output of sha1 needs to be redirected into a new file, but this is were my code is failing and I cannot, for the life for me, figure out why.

print "Input file names to be encrypted"; @files = <STDIN>; foreach $file (@files) { print "Preforming sha1 on $file"; system( "sha1sum $file > data" ); }

Any help on this would be tremendously appreciated.

P.S. Sorry of there is something wrong with my post, first time I've needed to ask my own question :p

Replies are listed 'Best First'.
Re: What is causing sha1 system call to fail to redirect?
by roboticus (Chancellor) on Jul 15, 2015 at 21:22 UTC

    Puregnome45:

    Since you're not chomping your input, you're actually executing two commands "sha1sum $file" and ">data". The second one generates an empty file....

    Update: changed "xyz" to "data" to reflect the name used in the OP.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: What is causing sha1 system call to fail to redirect?
by toolic (Bishop) on Jul 15, 2015 at 20:29 UTC
    You need to elaborate on "fail to redirect" and "my code is failing". Do you not get an output file? Is the output file empty?
    @files = <STDIN>;
    You don't show how you are running your code, but that line might be causing your code to appear to hang until you enter Ctrl-C, or some such. Also, you keep clobbering your output "data" file for each input file. Maybe you want something more like:
    use warnings; use strict; for my $file (@ARGV) { print "Preforming sha1 on $file\n"; system( "sha1sum $file >> data" ); }

    Run it like:

    code.pl file1 file2 file3

      Fair point, and thanks for remaining me. I meant to mention this in the actual post, but it is showing the output in the console; that is the file is completely empty.

      I'll give it like 10 or 15 files to encrypt and it prints sha1's output to the console and still creates the file 'data,' just empty.

      I'm not at work to actually test that fix, but I will be sure to first thing in the morning. I'd do it here at home, but I don't have Perl installed, and I'm only going to be working with Perl for another few weeks.

      Thanks for the speedy response!

Re: What is causing sha1 system call to fail to redirect?
by marinersk (Priest) on Jul 16, 2015 at 03:37 UTC

    For future reference (as you already have the answer from roboticus), here's a basic debugging technique which can open your eyes to the effects that unexpected data can have on your code: Display what you have, not what you think you have.

    Example: Your approach to displaying the information:

    print "Preforming sha1 on $file"; system( "sha1sum $file > data" );

    Produces:

    Preforming sha1 on test1.dat

    This is fine for human user display, but for programmer debugging display, you've left yourself open to a number of "dirty screen tricks". Instead:

    my $systemCommand = "sha1sum $file > data"; print "\$systemCommand = [$systemCommand]\n"; system( $systemCommand );

    Produces:

    $systemCommand = [sha1sum test.dat > data]

    Notice how the newline jumps out at you when displayed this way.

    I would be remiss to not note that there are two modules folks love to use which might also reveal these problems and more -- Data::Dumpand Data::Dumper. I've seen the effectiveness at these modules helping Monks zero in on a user's issues as if they were painted with red Xs. I highly recommend their use whenever you want to peek at your data in more depth.

Re: What is causing sha1 system call to fail to redirect?
by locked_user sundialsvc4 (Abbot) on Jul 15, 2015 at 20:47 UTC

    Here’s just-a-guess, but I betcha it’s a pretty good guess ...

    Your shell-command uses a single greater-than sign ... instead of two.

    That is to say, it says shasum $file > data, rather than shasum $file >> data

    Therefore, each command is replacing the file named data every time, instead of appending to it, as you probably intend.