in reply to Generating and storing regexp

If you're trying to generate a data structure to be loaded and used later, you might want to use Storable, which is designed for storing and loading data structures: perl -MStorable -le 'store { x => qr/x/ }, "/tmp/x"; $x = retrieve "/tmp/x"; print $x->{x}'

Replies are listed 'Best First'.
Re^2: Generating and storing regexp
by Eliya (Vicar) on Feb 20, 2011 at 15:40 UTC

    AFAICT, Storable can't store regexes. At least I do get "Can't store REGEXP items at ..." when I try to run your code.  (Also listed under BUGS in the docs)

    (Storable 2.22 that comes with Perl 5.12.2)

      Iinteresting...
      $ perl -MStorable -le 'print $Storable::VERSION, " ", $^V' 2.25 v5.10.0 $ perl -MStorable -le 'store { x => qr/x/ }, "/tmp/x"; $x = retrieve " +/tmp/x"; print $x->{x}' Regexp=SCALAR(0x10082e9f0) $ perl -MStorable -le 'store { x => qr/x/ }, "/tmp/x"; $x = retrieve " +/tmp/x"; print qq{!${$x->{x}}!}' !!
      So the latest Storable *pretends* to store it, but in fact gives you garbage. Cute.

      I can confirm that. Found out the hard way... :)