Is anything like this even doable?

Don't know what pabulum Pythonistas are used to, but for Perl we all know the answer to that ...

Anyway, if you are going to use regexes then absolutely make sure that when you set your $url you do use single quotes so that any variable-looking entities in there do not get interpolated. The way you do it in your example $GUID will be interpolated to empty, and because you do not use strict/warnings you will not even know it when it disappears.

Secondly, if you use regexes, your "variables" inside the $url are not necessary to look like perl variable names (e.g. $GUID). You can adopt any convention which makes the regex substitution easy and unambiguous. For example, and I am only trying to make a point by being extreme, you can use my $url = "http://something.com/api_stuff/<<<<VAR:GUID>>>>"; (note: single or double quotes in this case do not matter - there are no "Perl-looking" vars in there).

Of course, it is also possible to use $GUID in $url (as you do) but you have some escaping to do. At least use ${GUID} to decrease ambiguity. But there is absolutely no reason to use the dollar-sign and that brings you back to my suggestion.

For the really specific use-case you provide, I see no reason why you should use a variable like $GUID in your $url and then have it substituted. You could have a base URL which various functions take, along with GUIDs and other params and then they form a valid url by appending etc. That is, each function knows what URL format it has to return inherently, inside the function (and not globally as you do), and does whatever transformations are necessary with whatever input is required in order to produce and return the valid URL.

Of course, there are lots and lots of ways to achieve what you want, this is just one since you mentioned regex.

bw, bliako


In reply to Re: Strings with undefined variables? by bliako
in thread Strings with undefined variables? by 23skiddoo

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.