Object-Oriented Perl: Astro::CoordsΒΆ

This example demonstrates interaction with object-oriented Perl libraries via Taco. The libraries Astro::Coords, Astro::Telescope and DateTime are used to perform some calculations related to the position of Mars over Jodrell Bank during the Queen’s speech on Christmas Day, 2010.

  • Construct DateTime object

    First we set up a Taco object running the default Taco server script implementation for Perl. The server is then instructed to load the DateTime module and to construct an instance of that class. A set of Python keyword arguments is given to the constructor and will be turned into a flattened list of keywords and values as required by the DateTime constructor. Finally the DateTime object’s strftime method is called to allow us to check that the date has been set correctly.

    from taco import Taco
    taco = Taco(lang='perl')
    
    taco.import_module('DateTime')
    qs = taco.construct_object('DateTime', year=2010, month=12, day=25,
                               hour=15, minute=0, second=0)
    print(
        qs.call_method('strftime', '%Y-%m-%d %H:%M:%S')
    )
    
    2010-12-25 15:00:00
    

    Note

    The actual DateTime object will be stored in an object cache on the server side. The TacoObject simply refers to it by an object number. When the TacoObject‘s __del__ method is called, a destroy_object action will be sent, allowing the object to be cleared from the cache.

  • Construct Astro::Coords object for Mars

    Next we import the Astro::Coords module and construct an object representing the coordinates of Mars. Since we may want to construct several similar objects, we use the constructor() convenience method to get a callable which we can use to call the class constructor.

    taco.import_module('Astro::Coords')
    coords = taco.constructor('Astro::Coords')
    mars = coords(planet='mars')
    print(
        mars.call_method('name')
    )
    
    mars
    
  • Construct Astro::Telescope object and apply it to Mars

    The Astro::Telescope class offers information about a number of telescopes. It has a class method which can be used to fetch a list of supported telescope identifiers. This Perl method needs to be called in list context so we specify context='list' in the method call. If you come across a function or method which requires a keyword argument called context, this facility can be disabled by setting the disable_context attribute of the Taco object, for example by specifying disable_context=True in its constructor.

    taco.import_module('Astro::Telescope')
    telescopes = taco.call_class_method('Astro::Telescope',
                                        'telNames', context='list')
    print('JODRELL1' in telescopes)
    
    True
    

    Now that we have confirmed that the Perl module knows about Jodrell Bank, we can set this as the location in our object representing Mars. The Python positional argument 'JODRELL1' to the construct_object() method is passed to the Perl constructor at the start of its list of arguments.

    In this example, construct_object() will return a TacoObject, but when this is passed to another Taco method — in this case call_method() — it will automatically be converted to a reference to the object in the cache on the server side.

    We also need to set the date and time, which we can do by calling the Astro::Coords object’s datetime method. However as we will want to be able to repeat this easily, we can use the convenience routine method() to get a Python callable for it. This can then be called with the object representing the date and time of the Queen’s speech, which is again automatically converted to a reference to the corresponding Perl object.

    Finally we can have our Astro::Coords object calculate the elevation of Mars for this time and place.

    mars.call_method('telescope',
                     taco.construct_object('Astro::Telescope', 'JODRELL1'))
    datetime = mars.method('datetime')
    datetime(qs)
    print('{0:.1f}'.format(
        mars.call_method('el', format='degrees')
    ))
    
    8.2
    
  • Investigate the transit time

    So Mars was above the horizon (positive elevation), but it was still pretty low in the sky. We can have Astro::Coords determine the transit time — the time at which it was highest. (In this method call, event=0 requests the nearest transit, either before or after the currently configured time.)

    mt = mars.call_method('meridian_time', event=0)
    print(
        type(mt)
    )
    print(
        mt.call_method('strftime','%H:%M')
    )
    
    <class 'taco.object.TacoObject'>
    12:52
    

    Note

    The Perl meridian_time method has returned an object, which is now being referred to by a TacoObject instance. Taco handles objects returned from functions and methods in the same way as objects explicitly constructed with construct_object().

    We can now set the Mars object’s time to the meridian time using our convenience callable, and find the corresponding elevation.

    datetime(mt)
    print('{0:.1f}'.format(
        mars.call_method('el', format='degrees')
    ))
    
    13.0
    
  • Check the distance to the Sun

    As a final example, we will calculate the distance (across the sky) between Mars and the Sun. First we construct an object representing the Sun’s position.

    sun = coords(planet='sun')
    print(
        sun.call_method('name')
    )
    
    sun
    

    Then, after setting the Sun object to the same time, we can request the distance between the two objects. Astro::Coords returns the distance as another object, but we can call its degrees method to obtain a value in degrees.

    sun.call_method('datetime', mt)
    print('{0:.1f}'.format(
        mars.call_method('distance', sun).call_method('degrees')
    ))
    
    9.9