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?

    Thanks,
    Appu
     
    Murugesh, Mar 16, 2005
    #1
    1. Advertising

  2. Murugesh <> wrote in news:G7XZd.20$W3.185
    @news.oracle.com:

    > Anyidea on the maximum length a string in perl can contain.


    There is no pre-defined limit. On the other hand, perl is subject to the
    limits imposed by the OS etc.

    > I cannot read the whole content of a file into a string,


    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?

    > where it trims off after a particular limit.


    What 'particular limit'?

    > Is there any way to read whole file into a single string?


    Yes.

    Sinan
     
    A. Sinan Unur, Mar 16, 2005
    #2
    1. Advertising

  3. * Murugesh wrote:
    >
    > 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.


    No, there isn't one.

    > Is there any way to read whole file into a single string?


    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().

    regards,
    fabian
     
    Fabian Pilkowski, Mar 17, 2005
    #3
  4. Murugesh

    Murugesh Guest

    Fabian,

    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 $/;


    Fabian Pilkowski wrote:
    > * Murugesh wrote:
    >
    >> 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.

    >
    >
    > No, there isn't one.
    >
    >
    >>Is there any way to read whole file into a single string?

    >
    >
    > 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().
    >
    > regards,
    > fabian
     
    Murugesh, Mar 17, 2005
    #4
  5. Murugesh wrote:
    >
    > Thanks ! that works!
    > My content is not a binary file.In Windows, I tried ,
    > my $str = do{ undef $/; <FH> };


    You shouldn't undef() a global variable like that, it will affect your whole
    program. You should use local() instead:

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


    > My question is,
    >
    > why the follwing code doesnt work,
    > $/="";
    > my $str=<FH>;
    >
    > How does the above code is different from undef $/;


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

    perldoc perlvar



    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Mar 17, 2005
    #5
  6. Murugesh

    Murugesh Guest

    John,
    Does local method also undefines $/.There is no undef inside the do
    block.


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

    >
    >
    > You shouldn't undef() a global variable like that, it will affect your
    > whole
    > program. You should use local() instead:
    >
    > my $str = do{ local $/; <FH> };
    >
    >
    >> My question is,
    >>
    >> why the follwing code doesnt work,
    >> $/="";
    >> my $str=<FH>;
    >>
    >> How does the above code is different from undef $/;

    >
    >
    > Because assigning a zero length string to the input record separator
    > variable
    > sets it to paragraph mode.
    >
    > perldoc perlvar
    >
    >
    >
    > John
     
    Murugesh, Mar 17, 2005
    #6
  7. * John W. Krahn wrote:
    > Murugesh wrote:
    > >
    > > Thanks ! that works!
    > > My content is not a binary file.In Windows, I tried ,
    > > my $str = do{ undef $/; <FH> };

    >
    > You shouldn't undef() a global variable like that, it will affect your whole
    > program. You should use local() instead:
    >
    > my $str = do{ local $/; <FH> };


    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 ;-)

    thanks,
    fabian
     
    Fabian Pilkowski, Mar 17, 2005
    #7
  8. * Murugesh wrote:
    >
    > 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 $/;


    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.

    regards,
    fabian
     
    Fabian Pilkowski, Mar 17, 2005
    #8
  9. Murugesh <> wrote:


    Please do not top-post. Learn how to compose a proper followup.


    > Thanks ! that works!


    > 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 $/;



    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 SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Mar 17, 2005
    #9
  10. Murugesh wrote:
    >
    > John W. Krahn wrote:
    >
    >> Murugesh wrote:
    >>>
    >>> Thanks ! that works!
    >>> My content is not a binary file.In Windows, I tried ,
    >>> my $str = do{ undef $/; <FH> };

    >>
    >> You shouldn't undef() a global variable like that, it will affect your
    >> whole
    >> program. You should use local() instead:
    >>
    >> my $str = do{ local $/; <FH> };

    >
    > Does local method also undefines $/.


    Yes.


    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Mar 17, 2005
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mitchua
    Replies:
    5
    Views:
    2,824
    Eric J. Roode
    Jul 17, 2003
  2. Sam
    Replies:
    3
    Views:
    14,200
    Karl Seguin
    Feb 17, 2005
  3. Replies:
    5
    Views:
    689
    John W. Kennedy
    Jan 11, 2007
  4. Peter
    Replies:
    5
    Views:
    13,442
    Peter
    Jul 23, 2009
  5. phanhuyich
    Replies:
    4
    Views:
    324
Loading...

Share This Page