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

I am very new to Perl and I am trying hard to figure out the perl ways. Could somebody please help me with this. What I have so far is:

#!/usr/bin/perl use strict; use warnings; use File::Copy; use File::Basename; print "SCAN PPID:\n"; my $input = <STDIN>; chomp $input; print "Make sure you confirm the PPID before you proceed \n"; print "PPID: $input \n\n"; my $output = <STDOUT>; print STDOUT "$input";

please help with a command or next steps on saving my output to this folder -> c:\users\lastgen\docs\saved_ppid.

I honestly do not know where to start after getting my output and on how to save it to a folder. Please help.

Thank you.

Lastgen Monk.

Replies are listed 'Best First'.
Re: Saving my output to a folder
by stevieb (Canon) on Apr 19, 2016 at 17:23 UTC

    The following will loop until the user first types in a PPID, then until they type 'y' to confirm it.

    It then writes the PPID to $outfile in the $dir directory.

    use strict; use warnings; my $outfile = 'ppid_file.txt'; my $dir = 'c:/users/lastgen/docs/saved_ppid'; my $input; my $ok = ''; while ($ok !~ /[Yy]/){ print "SCAN PPID: "; $input = <STDIN>; chomp $input; print "Make sure you confirm the PPID before you proceed...\n\n"; print "PPID: $input \n\n"; print "ok? [y/n]: "; $ok = <STDIN>; } open my $wfh, '>', "$dir/$outfile" or die "can't open the damned output file! $!"; print $wfh $input;
      Thank you so much. I really appreciate. You're a lifesaver.
        If you will be running this script multiple times, each run will overwrite the previously saved ppid.

        If you want to preserve/append to the PPID instead, use this construct to open the file in "append" mode:

        open my $wfh, '>>', "$dir/$outfile" or die "ERROR: Can't open/append the damned output file! The OS says +:$!";

                This is not an optical illusion, it just looks like one.

      Steveib, Is it possible I can name the output text file in the destination folder as whatever text or number that is scanned or entered? for example if I scan or enter the PPID as ABCDEDEF..., can auto-save the text file as scanned item's name i.e. ABCDEDEF.... and so on? At least how can save each scanned item as a new txt file in the destination folder?
Re: Saving my output to a folder
by Marshall (Canon) on Apr 19, 2016 at 17:44 UTC
    This part,
    my $output = <STDOUT>; print STDOUT "$input";
    is not right. You are trying to read from the output! And print goes by default to STDOUT which in this case is the terminal.

    You need to open a file handle for output like this:

    open OUT, '>', 'c:/users/lastgen/docs/saved_ppid' or die "unable to open out file $!"; print OUT "something";
    Note that I used "forward slash" instead of "back slash" in the path name. This is completely fine on Windows and will save yourself a lot of pain because "\" means something very special in Perl.

    Update: If c:/users/lastgen/docs/saved_ppid is a directory then of course you need a target file like: c:/users/lastgen/docs/saved_ppid/ppid.txt. I see stevieb posted while I was typing. That code shows the preferred way for a file handle, using a lexical variable ($wfh) instead of what I did, a bare word (OUT). I did it that way because it might be easier for you to get started with and look at bit more obvious.

      Marshall, Thank you too. I am glad we have great people like you guys on this site. Blessed be you all. Thanks a million times to all of you.
        I wish you well on your Perl journey! You did a bunch of stuff right including: a) asking a clear question, b)showing some code that shows that you are making an attempt. Whether or not your code works is not that important. I mean you wouldn't be asking for help if everything worked. Right? My theory is that there is a finite "quanta" of information that can be transferred and absorbed in one thread. I think we are there. Go forth and write more code!