Does Perl provide this functionality?

Yes. As I demonstrated above. And here again:

c:\test>perl -le" my $n = oct( $ARGV[ 0 ] ); printf qq[As decimal: %d As octal: %o \n], $n, $n; " 0644 As decimal: 420 As octal: 644

Let me try and explain what is going on above/

  1. The value '0644' is a string being supplied to the perl program as a command line argument. It shows up inside the program (the bit between double quotes here: -le"..."), as $ARGV[ 0 ].
  2. $ARGV[0] is then passed to the built-in function oct, which parses that string ('0644'), and converts it to a number in perl's internal binary representation, and assigns it to the scalar variable $n.
  3. Now, when you use $n, how it will be used will depend upon the context in which you use it.

    That is to say, if you use $n as a string--for example by concatenating it to another string:

    c:\test>perl -le"my $n = oct( $ARGV[ 0 ] ); print 'As a string:' . $n; + " 0644 As a string:420

    It will be converted from that internal binary representation automatically, and used as a (decimal) formatted ascii string.

    However, if you use it in a numeric context--by adding it to another number--then it will be used as a number:

    c:\test>perl -le"my $n = oct( $ARGV[ 0 ] ); print 1 + $n; " 0644 421

Does that clarify things for you?

I realise that if you are used to having to convert between string and numeric representations explicitly, that this automation seems unintuative and leaves you thinking you need to do more, but trust me it works.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
RIP PCW It is as I've been saying!(Audio until 20090817)

In reply to Re^3: String to decimal to octal by BrowserUk
in thread Convert string to decimal to octal by patt

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.