Not sure if this is the forum for this, but it's one of those little things that makes me love Perl. I had written a script to make a task easier for a group of project managers, and it's on the file server for them to play with. Project Managers being dirty, they could move the script or change the name at whim. It requires a file to be configured for their specific task, and I wanted the default config file (if not specified) to be <scriptname>.cfg, without the .pl extension. What makes me stare at this line of code and get watery-eyed is that I'm using a function in both scalar and list context, in a temporally unsound way. Split is being referenced in a literal context by the @_ default array created by treating split in a list context.
This hurts my brain. =)
substr(($script = $0 = (@_[split(/\\/,$0)-1])),-3) = ".cfg";
$0 = whatever.pl
$script = whatever.cfg

Replies are listed 'Best First'.
Re: default config file
by merlyn (Sage) on May 04, 2001 at 19:38 UTC
(bbfu) Re: default config file
by bbfu (Curate) on May 09, 2001 at 07:27 UTC

    I second merlyn's suggestion to use File::Basename for three reasons:

    1. What if they decide to turn on -w (like they should)? They get a "Use of implicit split to @_ is deprecated" warning. That's no fun.
    2. What if they move to a system that doesn't use \ as the directory separating character (like UNIX)? It could happen, and your code doesn't handle /'s. Of course, not chopping off the path would cause the config file to always be stored in the same directory as the script, instead of where ever the script happens to be called from, which might not be such a bad thing after all...
    3. What if they change the name of the script to something that doesn't have a .pl extension (more common on *nix systems)? Your code clobbers the name of the script. For example, if they called their script whatever, your config file would become whate.cfg and I don't think that's what you mean.

    merlyn's regexp/File::Basename solution handles all of these cases and it's quite a bit easier to understand. Plus it doesn't modify $0 which could be useful (or not, depending; you can always modify his to do so).

    All in all, that certainly was an interesting way of using split and it took me a minute to figure out what you were doing. :-)

    Cheers.

    bbfu
    Seasons don't fear The Reaper.
    Nor do the wind, the sun, and the rain.
    We can be like they are.

      All of your points are quite valid, and had I taken the time to find Base::Filename (I am still merely an egg), I would have used it. This was a specific little hack for a specific little instance, where this will always be on a windows system, and the project managers would be expecting it to pick up their config file from the directory they're running the script in. (most of them can only run the script from looking at their notes which include everything down to directory navigation) It was also going to be replaced soon with a web interface for performing this task.

      I was basically trying to play around and make the default config file in this specific instance variable to the name of the script being run. Yet mostly it was an exercise in building 10 commands into 1 line and enjoying myself =) In retrospect, I should learn from this to:
      • account for as many variables as possible with my code
      • check for a module which performs my tasks cleanly on CPAN
      • post absurd code snippets like that under Obfuscation
      I also need to look through the FAQ's here and learn how to easily include all the proper links in my posts. I feel like such a slacker =)

      I still have not nearly the programming experience I would like,
      and a childs eyes when I play with Perl.

      -=rev=-
      When you hear sweet syncopation
      And the music softly moans
      T'ain't no sin to take off your skin
      And dance around in your bones