Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: To glob or not to glob

by Tux (Abbot)
on Jan 11, 2018 at 07:52 UTC ( #1207102=note: print w/replies, xml ) Need Help??

in reply to To glob or not to glob

Using it the way you describe will cause porting issues for older perl versions:

$ perl -MFile::Glob::bsd_glob -wE'say for bsd_glob ("*.foo")' Can't locate File/Glob/ in @INC (@INC contains: /pro/lib/pe +rl5/site_perl/5.14.2/IA64.ARCHREV_0-LP64-ld /pro/lib/perl5/site_perl/ +5.14.2 /pro/lib/perl5/5.14.2/IA64.ARCHREV_0-LP64-ld /pro/lib/perl5/5. +14.2 .). BEGIN failed--compilation aborted.

edit: correct above example of failure. The correct syntax is just as bad though:

$ perl -MFile::Glob=:bsd_glob -wE'say for bsd_glob ("*.foo")' "bsd_glob" is not defined in %File::Glob::EXPORT_TAGS at /pro/lib/perl +5/5.14.2/IA64.ARCHREV_0-LP64-ld/File/ line 45. File::Glob::import("File::Glob", ":bsd_glob") called at -e lin +e 0 main::BEGIN() called at -e line 0 eval {...} called at -e line 0 Can't continue after import errors at -e line 0. BEGIN failed--compilation aborted.

And additional shit hits the fan when you would try to install it

$ cpan File::Glob : The most recent version "1.28" of the module "File::Glob" is part of the perl-5.26.1 distribution. ...

Does this in your opinion imply that File::Glob should be dual-lived?

The current (today) absolute advised minimum version to be supported by the toolchain is perl-5.8.1. My example with 5.14.2 could arguably be considered recent enough.

Digging in the Delta's:

perl561delta.pod: File::Glob::glob() has been renamed to File::Glob::bsd_glob() perl58delta.pod: File::Glob::glob() has been renamed to File::Glob::bsd_glob() perl5160delta.pod: It has a new C<:bsd_glob> export tag, intended to replace C<:glob>. perl5260delta.pod: L<C<File::Glob::glob()> will disappear in perl 5.30. Use C<File::Glo +b::bsd_glob()> instead.

Does *not* make me happy. I see the trouble that is addressed, but this makes portable programming much much harder. Looks like the only portable way to write it from 5.6.1 to blead is:

use File::Glob; my @files = File::Glob::bsd_glob ("*.txt");

Safe, but not really a nice replacement of the simple <*.txt> or glob ("*.txt").

Enjoy, Have FUN! H.Merijn

Replies are listed 'Best First'.
Re^2: To glob or not to glob
by haukex (Abbot) on Jan 11, 2018 at 12:09 UTC

    Thank you for the reply!

    Using it the way you describe will cause porting issues for older perl versions

    You are correct that the :bsd_glob export tag wasn't added until Perl v5.16, sometime from File::Glob 1.13 to 1.17 in File::Glob 1.15. (Note you've got a typo in your example, -MFile::Glob::bsd_glob. fixed) But from the File::Glob docs:

    The :glob tag, now discouraged, is the old version of :bsd_glob. It exports the same constants and functions, but its glob() override does not support iteration; it returns the last file name in scalar context.

    So one backwards-compatible way to go is use File::Glob ':glob';, unless you want to use it in scalar context, which might not be a good idea anyway due to the issue choroba described.

    $ touch 'foo bar.txt' $ perl -MFile::Glob=:glob -e 'print for <*foo bar*>'

    This works in Perl 5.6.2 thru 5.24, and warns about the deprecation of :glob in 5.26. What you can do in Perl v5.6 thru v5.26 (and hopefully beyond) is either

    use File::Glob 'bsd_glob'; print for bsd_glob('*foo bar*'); # -- or -- use File::Glob $] lt '5.016' ? ':glob' : ':bsd_glob'; print for <*foo bar*>;

    With the limitation of not being able to use the latter in scalar context until Perl v5.16 and up (Update: and the former not at all, unfortunately).

    Does this in your opinion imply that File::Glob should be dual-lived?

    No, I don't yet have a well-formed opinion either way.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1207102]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2018-02-24 10:41 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (310 votes). Check out past polls.