Maximum string length in perl

Discussion in 'Perl Misc' started by Murugesh, Mar 16, 2005.

  1. Murugesh

    Murugesh Guest

    hi all,

    Anyidea on the maximum length a string in perl can contain.I
    cannot read the whole content of a file into a string,where it trims off
    after a particular limit.Is there any restriction.
    Is there any way to read whole file into a single string?

    Murugesh, Mar 16, 2005
    1. Advertisements

  2. There is no pre-defined limit. On the other hand, perl is subject to the
    limits imposed by the OS etc.
    How about posting a short, complete example that exhibits the problem? How
    about telling us the size of the file you are trying to read?
    What 'particular limit'?

    A. Sinan Unur, Mar 16, 2005
    1. Advertisements

  3. No, there isn't one.
    I prefer to use

    my $str = do{ undef $/; <FH> };

    but on windowish systems this could be

    my $str = do{ undef $/; binmode FH; <FH> };

    to read files with binary content correctly. From `perldoc -f binmode`:

    Another consequence of using binmode() (on some systems) is that
    special end-of-file markers will be seen as part of the data stream.
    For systems from the Microsoft family this means that if your binary
    data contains \cZ, the I/O subsystem will regard it as the end of
    the file, unless you use binmode().

    Fabian Pilkowski, Mar 17, 2005
  4. Murugesh

    Murugesh Guest


    Thanks ! that works!
    My content is not a binary file.In Windows, I tried ,
    my $str = do{ undef $/; <FH> };
    My question is,

    why the follwing code doesnt work,
    my $str=<FH>;

    How does the above code is different from undef $/;
    Murugesh, Mar 17, 2005
  5. You shouldn't undef() a global variable like that, it will affect your whole
    program. You should use local() instead:

    Because assigning a zero length string to the input record separator variable
    sets it to paragraph mode.

    perldoc perlvar

    John W. Krahn, Mar 17, 2005
  6. Murugesh

    Murugesh Guest

    Does local method also undefines $/.There is no undef inside the do
    Murugesh, Mar 17, 2005
  7. Err... this is the second time I'm doing this mistake. Each time it was
    in a newsgroup so that everyone could see my inattention ;-( Of course,
    I want to localize that var ...

    But also, each time one corrects my intention a few minutes later ;-)

    Fabian Pilkowski, Mar 17, 2005
  8. Please take notice what John has posted. Use local() instead of undef().
    But in

    my $str = do{ local $/; <FH> };

    the value of $/ is *undef* since I don't assign any value. In your case
    $/ contains an empty string, which isn't the same. All of these values
    are described in `perldoc perlvar`. Have a look at it.

    Btw, please localize your $/-assignment into an own block, otherwise
    this will affect your whole program.

    Fabian Pilkowski, Mar 17, 2005
  9. Please do not top-post. Learn how to compose a proper followup.

    If you are wondering about the values of the $/ variable, then
    you should look up the $/ variable in the docs that came with perl:

    perldoc perlvar

    treating empty lines as a terminator if set to
    the null string.
    You may set it ... to undef to read through the end of file.

    So, setting it to the empty string (para mode) reads records
    that are separated by blank lines (and your code above only
    does one read, it never looks at the rest of the file).

    Setting it to undef (slurp mode) reads the *entire file*
    in one input operation.

    [snip TOFU]
    Tad McClellan, Mar 17, 2005
  10. Yes.

    John W. Krahn, Mar 17, 2005
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.