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

Dear SuperMonks, I'm very new to Perl w/ no legitimate programming background. That said, I looked at the strings and files_and_directories tutorials but didn't see the answer there.

I am trying to fetch the files off of numerous similar pages such as this and want the script to save/catalog those files as "gid_2007_08_06_quiaaa_yucaaa_1_112039.xml", etc. instead of just "112039.xml" which is the only way I know how to script it to save at the moment. I want to catalog in the new fashion because there are numerous "gids" (game IDs as shown above) for each batter ID (ie 112039).

Here is the snippet of code that is resulting in the files downloading only as ".xml":

open PLYRS, ">$outputdir\\$game_players.xml"

The files save to $outputdir correctly but I don't know what to replace '$game_players.xml' with to achieve the desired results as described above.

What strategy might you recommend or tutorial might there be to learn how to do this? Thanks!

Replies are listed 'Best First'.
Re: How to Save Fetched Web Files as "path/$string.xml"
by GrandFather (Saint) on Aug 18, 2007 at 04:50 UTC

    Where does '2007_08_06_quiaaa_yucaaa_1' come from and what does it look like in it's "raw" form? The trite answer to your question is: Just interpolate the string. (See Quote Like Operators for a discussion of interpolation.)

    Note that the three parameter open is much preferred over the two parameter version that you have used and you should always check the result of the open so errors are caught sooner rather than later. You should use $! (see perlvar) to include the system error in any diagnostic message you generate as a result.

    Update: fix system error variable!


    DWIM is Perl's answer to Gödel
      '2007_08_06_quiaaab_yucaaa_1' is the end part of the web url to which I linked and a unique way to identify each game. I will look into interpolation and these other things. Much thanks as always.
      OK, I believe that interpolation is what I tried to do with the string but my question was what strategy best executes that interpolation. I don't even know for sure that a filename output may contain a string. $game in the filename = that portion of the url that I emphasized earlier.

        The following sample may help clear up the interpolation issue. Don't worry too much about the first few lines. The key stuff is inside the for loop. The regex pulls the two parts out of the URL that you want to munge together and the double quoted string does the munging by interpolating the contents of the variables into the string. Note the use of the {} to let the Perl interpreter know that the trailing _ is not part of the variable name.

        use warnings; use strict; my $root = 'http://gd2.mlb.com/components/game/aaa/year_2007/month_08/ +day_06/gid_2007_08_06_quiaaa_yucaaa_1/batters/'; my @batters = map {"$root$_"} qw(112039.xml 120107.xml); for my $url (@batters) { my ($prefix, $file) = $url =~ m!/(gid\w*)/batters/(.*)!; print "${prefix}_$file\n"; }

        Prints:

        gid_2007_08_06_quiaaa_yucaaa_1_112039.xml gid_2007_08_06_quiaaa_yucaaa_1_120107.xml

        The double quoted string following the print can drop into your open, but remove the \n at the end and remember to use the three parameter version and to check the result.


        DWIM is Perl's answer to Gödel